/**
 Haydonian SuperTranslator - caesar_shift.js
 Copyright (c) 2013, Alex Elzenaar
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
     * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
     * The name of the author may not be used to endorse or promote products
       derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL ALEX ELZENAAR BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/

// Alphabets
var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var lower  = "abcdefghijklmnopqrstuvwxyz";

// For convenience
String.prototype.replaceAt = function(index, char)
{
  return this.substr(0, index) + char + this.substr(index + char.length);
}

// Shift a character with the given alphabet
// alphabet - the alphabet to use.
// index - the index of the character to shift.
// shift - the amount to shift the character (can be negative).
var genericShift = function(alphabet, index, shift)
{
  var shiftedIndex = index + shift; // Get the index of the shifted character

  // If we went past the end of the alphabet, wrap back to 0.
  if(shiftedIndex == alphabet.length) shiftedIndex = 0;
   // If we went past the beginning of the alphabet, wrap to the end.
  else if(shiftedIndex == -1) shiftedIndex = (alphabet.length - 1);

  // Get the shifted character.
  encrypted = alphabet.charAt(shiftedIndex);
  return encrypted; // Return the shifted character.
}

// Perform a caesar shift of a string (using the upper & lower alphabets)
// string - the string to shift.
// shift - the amount to shift each character (can be negative).
var caesarShift = function(string, shift) 
{
  for(var i = 0; i < string.length; i++) // iterate over string
  {
    var current = string.charAt(i); // Get the current character.

    // Get the index of the character in both the upper and lower alphabets.
    var lowIndex = lower.indexOf(current);
    var uppIndex = upper.indexOf(current);
    if(uppIndex != -1) // Character is uppercase
    {
      current = genericShift(upper, uppIndex, shift); // Shift it!
    }
    else if(lowIndex != -1) // Character is lowercase
    {
      current = genericShift(lower, lowIndex, shift); // Shift it!
    }
    else // Character is not in either alphabet
    {
      // Don't transform it.
    }

    // Replace the old character with the shifted character.
    string = string.replaceAt(i, current);
  }

  return string;
}

